﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static User_manage.User_Lgon;

namespace Alarm_interface
{
    public partial class AlarmControl : UserControl
    {
		//权限
		public  H_Level User_Level;

		public DataGridView _dataTable { set { Alarm_dataGridView = value; } get { return Alarm_dataGridView; } }


		public DataTable[] Alarm_dataTable=new DataTable[8];

		//事件
		public delegate void TableUpdate_Click(object sender, EventArgs e);
		public event TableUpdate_Click _TableUpdate_Click;

		#region //标题设置
		public string Title_Name
		{
			get { return label1.Text; }
			set { label1.Text = value; }
		}
		public Color Title_FontColor
		{
			get { return label1.ForeColor; }
			set { label1.ForeColor = value; }
		}
		public Color Title_BackColor
		{
			get { return label1.BackColor; }
			set { label1.BackColor = value; }
		}
		public Font Title_Font
		{
			get { return label1.Font; }
			set { label1.Font = value; }
		}

		//public int Title_Height
		//{
		//	get { return label1.Height; }
		//	set { label1.Height = value; }
		//}

		#endregion

		private ContextMenuStrip CMenuStrip;
		public AlarmControl()
        {
            InitializeComponent();
			SetStyle(ControlStyles.ResizeRedraw | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);
			UpdateStyles();

			dataGridView_init();
			BindMenu();
			ContextMenuStrip = CMenuStrip;

            updateData();


        }

		//protected override CreateParams CreateParams
		//{
		//	get
		//	{
		//		CreateParams cp = base.CreateParams;
		//		cp.ExStyle |= 0x02000000;
		//		return cp;
		//	}
		//}

		private void AlarmControl_Load(object sender, EventArgs e)
        {
			;
        }

		public void AlarmInfo_Add(string timer, string Station, string AlarmID, string AlarmInfo, Color? _Color = null)
		{
			for (int i = 0; i < Alarm_dataGridView.Rows.Count; i++)
			{
				if (Alarm_dataGridView.Rows[i].Cells[2].Value.ToString() == AlarmInfo)
				{
					return;
				}
			}

			DataGridViewRowCollection rows = Alarm_dataGridView.Rows;
			object[] values = new string[4] { timer, Station, AlarmID, AlarmInfo };
			rows.Add(values);
			Alarm_dataGridView.Rows[Alarm_dataGridView.Rows.Count - 1].DefaultCellStyle.SelectionForeColor = _Color?? Color.Red;
			Alarm_dataGridView.Rows[Alarm_dataGridView.Rows.Count - 1].DefaultCellStyle.ForeColor = _Color ?? Color.Red;
		}

		public void AlarmInfo_delete(string AlarmID)
        {
            foreach (DataGridViewRow dr in Alarm_dataGridView.Rows)
            {
				if (dr.Cells[2].Value.ToString() == AlarmID)
					Alarm_dataGridView.Rows.Remove(dr);
			}
        }
		public void dataGridView_init()
        {
			//优化dataGridView控件闪耀现象
			Type dgvType = Alarm_dataGridView.GetType();
			PropertyInfo pi = dgvType.GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic);
			pi.SetValue(Alarm_dataGridView, true, null);



			//字体大小编辑
			Alarm_dataGridView.ColumnHeadersDefaultCellStyle.Font = new Font("宋体", 10.5f, Font.Style | FontStyle.Regular);
			Alarm_dataGridView.DefaultCellStyle.Font = new Font("宋体", 10.5f, Font.Style | FontStyle.Regular);

			//禁止编辑
			Alarm_dataGridView.ReadOnly = true;
			//不显示空白新增行
			Alarm_dataGridView.AllowUserToAddRows = false;
													 //添加字段名
			string[] title_str = new string[] { "时间", "报警工位", "报警ID", "报警信息" };
			int[] title_width = new int[] { 170,200, 100, 5000 };//比例
														 //增加列头列名称
			Alarm_dataGridView.ColumnCount = title_str.Length;
			Alarm_dataGridView.ColumnHeadersVisible = true;
			DataGridViewCellStyle code = new DataGridViewCellStyle();
			code.BackColor = Color.Beige;
			code.Font = new Font("Verdana", 10, FontStyle.Bold);

			if (title_str.Length > 0)
			{
				for (int i = 0; i < title_str.Length; i++)
				{
					Alarm_dataGridView.Columns[i].Name = title_str[i];
					Alarm_dataGridView.Columns[i].Width = title_width[i];
					//Alarm_dataGridView.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
					//Alarm_dataGridView.Columns[i].FillWeight = title_width[i];
				}
			}
			//禁止排序
			if (Alarm_dataGridView.Columns.Count > 0)
			{
				for (int i = 0; i < Alarm_dataGridView.Columns.Count; i++)
				{
					Alarm_dataGridView.Columns[i].SortMode = DataGridViewColumnSortMode.Programmatic;
				}
			}
			//禁止用户改变列宽行高
			Alarm_dataGridView.AllowUserToOrderColumns = false;
			Alarm_dataGridView.AllowUserToResizeRows = false; 
			Alarm_dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
			Alarm_dataGridView.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;

		}

		private void BindMenu()
		{
			CMenuStrip = new ContextMenuStrip();
			ToolStripItem toolStripItem = new ToolStripMenuItem();
			toolStripItem.Name = "编辑报警信息ToolStripMenuItem";
			toolStripItem.Text = "编辑报警信息";
			toolStripItem.Click += 编辑报警信息ToolStripMenuItem;
			CMenuStrip.Items.Add(toolStripItem);

		}


		Alarm_editing _alarm_Editing = null;
		private void 编辑报警信息ToolStripMenuItem(object sender, EventArgs e)
        {
			if (User_Level != H_Level.管理员)
			{
				MessageBox.Show("权限错误，请切换为管理员权限", "错误提示");
				return;
			}
			_alarm_Editing = new Alarm_editing();
            _alarm_Editing.FormClosed += _alarm_Editing_FormClosing;
			_alarm_Editing.ShowDialog(this);
			;
        }

		//关闭编辑报警信息窗体后重新获取最新的报警信息
        private void _alarm_Editing_FormClosing(object sender, FormClosedEventArgs e)
        {
            updateData();

            _alarm_Editing.FormClosed -= _alarm_Editing_FormClosing;
			_TableUpdate_Click?.Invoke(sender, e);
			;
		}

        public T Deserialize<T>(string file_path)
		{
			try
			{
				using (FileStream fs = new FileStream(file_path, FileMode.Open))
				{
					BinaryFormatter binaryFormatter = new BinaryFormatter();
					return (T)binaryFormatter.Deserialize(fs);
				}
			}
			catch
			{
				return default(T);
			}
		}


        //
        private void updateData()
        {
            DataTable buf = Deserialize<DataTable>(Application.StartupPath + "\\Config\\P1_AlarmInfo.config");
            if (buf != null)
            {
                Alarm_dataTable[0] = buf;
            }

            buf = Deserialize<DataTable>(Application.StartupPath + "\\Config\\P2_AlarmInfo.config");
            if (buf != null)
            {
                Alarm_dataTable[1] = buf;
            }
            buf = Deserialize<DataTable>(Application.StartupPath + "\\Config\\P3_AlarmInfo.config");
            if (buf != null)
            {
                Alarm_dataTable[2] = buf;
            }
            buf = Deserialize<DataTable>(Application.StartupPath + "\\Config\\P4_AlarmInfo.config");
            if (buf != null)
            {
                Alarm_dataTable[3] = buf;
            }
            buf = Deserialize<DataTable>(Application.StartupPath + "\\Config\\P5_AlarmInfo.config");
            if (buf != null)
            {
                Alarm_dataTable[4] = buf;
            }
            buf = Deserialize<DataTable>(Application.StartupPath + "\\Config\\P6_AlarmInfo.config");
            if (buf != null)
            {
                Alarm_dataTable[5] = buf;
            }
            buf = Deserialize<DataTable>(Application.StartupPath + "\\Config\\P7_AlarmInfo.config");
            if (buf != null)
            {
                Alarm_dataTable[6] = buf;
            }
            buf = Deserialize<DataTable>(Application.StartupPath + "\\Config\\P8_AlarmInfo.config");
            if (buf != null)
            {
                Alarm_dataTable[7] = buf;
            }

        }

    }
}
